import { createSlice } from '@reduxjs/toolkit'
import { Toast } from 'antd-mobile'

export const userlist = createSlice({
    name: 'userlist',
    initialState: {
        userpope: [
            {
                id: 1,
                name: '瑶瑶',
                code: '241222200606205540',
                state: false
            },
            {
                id: '2',
                name: '涛涛',
                code: '3424125435153224234',
                state: false
            },
            {
                id: 3,
                name: '迪丽热巴',
                code: '341222200606205540',
                state: false
            },
            {
                id: 4,
                name: '巴铁小伙',
                code: '341222200606205540',
                state: false
            },
        ]

    },
    reducers: {
        // // 修改乘车人的状态
        // editcityle(state, { payload }) {
        //     const item = state.userpope.find((item) => item.id === payload.id)
        //     if (item) {
        //         item.state = !item.state
        //     }
        // },
        // 删除乘车人
        detilpepo(state, { payload }) {
            state.userpope = state.userpope.filter((item) => item.id !== payload.id)

        },

        //添加乘车人
        addpepo(state, { payload }) {
            const item = state.userpope.find((item) => item.id === payload.id)
            if (!item) {
                state.userpope.push(payload)
                Toast.show({
                    icon: 'success',
                    content: '添加成功'
                })
            }
            else {
                Toast.show({
                    icon: 'success',
                    content: '已有此人'
                })
            }
        },

        //  修改乘车人
        editpope(state, { payload }) {
            const item = state.userpope.find((item) => item.id === payload.id)
            if (item) {
                Object.assign(item,payload)
                Toast.show({
                    icon: 'success',
                    content: '编辑成功'
                })
            }
          
        }

    }
})
// 每个 case reducer 函数会生成对应的 Action creators
export const { detilpepo, addpepo, editpope } = userlist.actions

export default userlist.reducer